package com.grp6.game.chess.model;

import java.util.List;

import com.grp6.game.chess.player.ChessHumanPlayer;
import com.grp6.game.chess.player.IChessPlayer;
import com.grp6.game.chess.view.ChessGameView;
import com.grp6.game.common.IObserver;
import com.grp6.game.common.InvalidMoveException;
import com.grp6.game.model.AbstractObservableModel;
import com.grp6.game.model.IGameModel;

public class ChessGameModel extends AbstractObservableModel<ChessGameView> implements IGameModel, IObserver{

	private ChessGameBoard board;
	private List<IChessPlayer> players;

	public ChessGameModel(ChessGameBoard board,List<IChessPlayer> players) {
		this.players = players;
		this.board = board;
	}


	@Override
	public ChessGameBoard getGameBoard() {
		return board;
	}


	@Override
	public void notifyInvalidMove(InvalidMoveException e) {
		for(ChessGameView v:getViews())
			v.notifyInvalidMove(e);	
	}


	@Override
	public List<IChessPlayer> getPlayers() {
		return players;
	}


	@Override
	public void actionPerformed(String actionName) {
		// TODO Auto-generated method stub
		
	}
	
	public void setupObservation(){
		for(ChessPlace p:board.getPlaces())
			p.getObservers().add(this);
	}


	@Override
	public void update() {
		IChessPlayer player = null;
		for(IChessPlayer p : players)
			if (p.isPlaying())
				player = p;
		
		for(ChessPlace place:board.getPlaces())
			if (place.isClicked()){
				place.setClicked(false);
				
				if (player instanceof ChessHumanPlayer)
					((ChessHumanPlayer)player).onPlaceHit(place);
			}
	}

}
